//
//  MixPoint.cpp
//  candyfactory
//
//  Created by luotianqiang1 on 16/4/25.
//
//

#include "MixPoint.h"

void MixPoint::setPointPath(const string& s){
    pointPath = s;
}

void MixPoint::run(float time) {
    delayTime = time;
    if(!isScheduled(schedule_selector(MixPoint::updateTime)))
        this->schedule(schedule_selector(MixPoint::updateTime));
}
void MixPoint::stop(){
    stopAllActions();
    unschedule(schedule_selector(MixPoint::updateTime));
}

void MixPoint::updateTime(float t){
    currentTime+=t;
    if(currentTime>delayTime){
        currentTime = 0;
        crateFunc();
    }
}


void MixPoint::crateFunc(){
    auto angle = 0;
    int num = 10;
    for(int i=0;i<num;i++){
        auto s = Sprite::create(pointPath);
        if(s != nullptr ) {
            s->setRotation(rand()%360);
            s->setScale(0.5+rand()%5*0.1);
            angle = angle+360/(num*1.0);
            auto pos = Vec2(0,distanceToCenter-10+rand()%20+getContentSize().height*.5);
            addChild(s);
            float time = delayTime*0.5;
            if(time>0.5)
                time = 0.5;
            else if(time<0.2)
                time = 0.2;
            
            auto src = Vec2(0,25);
            
            pos.rotate(getContentSize()*.5, angle);
            if(_isEllipse) {
                float x = 0, y = 0;
                float k = tanf(angle);
                float  _a = 190;
                float _b = 60;
                if (angle%360 <= 90) {//第一象限
                    x = (_a * _b) / sqrt(pow(k, 2) * pow(_a, 2) + pow(_b, 2));
                    y = k * x;
                }
                else if(angle%360 <= 180){//第二象限
                    x = -(_a * _b) / sqrt(pow(k, 2) * pow(_a, 2) + pow(_b, 2));
                    y = k * x;
                }
                else if(angle%360 <= 270){//第三象限
                    x = -(_a * _b) / sqrt(pow(k, 2) * pow(_a, 2) + pow(_b, 2));
                    y = k * x;
                }
                else{//第四象限
                    x = (_a * _b) / sqrt(pow(k, 2) * pow(_a, 2) + pow(_b, 2));
                    y = k * x;
                }
                pos =  getContentSize()*.5+Size(x-7+rand()%14, y-7+rand()%14);
                s->setPosition(pos);
                s->runAction(FadeTo::create(time, 0));
                
                auto direction = Vec2(pos - getContentSize()*.5);
                auto moveByPos =  direction*0.2;
                s->runAction(Sequence::create(MoveBy::create(time, moveByPos),RemoveSelf::create(),nullptr));
            }else {
                s->setPosition(pos);
                src.rotate(Vec2::ZERO, angle);
                s->runAction(Sequence::create(MoveBy::create(time, src),RemoveSelf::create(),nullptr));
                s->runAction(FadeTo::create(time, 0));
            }
        }
        
    }
}

MixPoint::MixPoint():pointPath("")
,distanceToCenter(150)
,delayTime(0.0)
,acTionTag(10)
,currentTime(0)
,_isEllipse(false){
    
}

bool MixPoint::init() {
    if(Node::init()) {
        setAnchorPoint(Vec2::ANCHOR_MIDDLE);
        return true;
    }
    return false;
}